package com.boot2.core.web.controller;

import com.boot2.core.dao.PageInfo;
import com.boot2.core.dao.i.SysDictDataDao;
import com.boot2.core.dao.i.SysDictTypeDao;
import com.boot2.core.model.SysDictData;
import com.boot2.core.model.SysDictType;
import com.boot2.core.model.vo.Result;
import com.boot2.core.service.SysDictDataService;
import com.boot2.core.web.BaseController;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangweilin
 * @ClassName: SysDictDataController
 * @Description: 【数据字典】 controller
 * @date 2020-6-17 12:18:17
 */
@Api(tags = "数据字典")
@RestController
@CommonsLog
@RequestMapping("/dict")
public class SysDictController extends BaseController {

    @Autowired
    SysDictDataService sysDictDataService;

    @Autowired
    SysDictDataDao sysDictDataDao;

    @Autowired
    SysDictTypeDao sysDictTypeDao;

    @GetMapping("/list/type")
    @ApiOperation(value = "查询字典类型列表",notes = "可分页")
    public Result<PageInfo<SysDictType>> listType() {
        Page<SysDictType> sysDictTypePage = sysDictTypeDao.findAll(jpaPageRequest());
        return page(sysDictTypePage);
    }

    /**
     * 查询字典列表
     *
     * @param type
     * @return
     */
    @GetMapping("/list/data/{typeList}")
    @ApiOperation(value = "查询字典数据列表",notes = "typeList可以为多个值，用逗号分开，如installation_way,communication_way")
    public Result<Map<String, ArrayList<SysDictData>>> listData(@PathVariable List<String> typeList) {
        List<SysDictData> sysDictDataList = sysDictDataDao.findByDictTypeInAndStatusOrderByDictSort(typeList, false);
        Map<String, ArrayList<SysDictData>> map = sysDictDataList.stream().collect(Collectors.toMap(e -> e.getDictType(), e -> Lists.newArrayList(e), (e1, e2) -> {
            e1.addAll(e2);
            return e1;
        }));
        return success(map);
    }




}
